use super::time::{UTime,SysTime,UClockData,SysConvertor,UDuration};
pub trait Asf64 {
    fn asf64(&self) -> f64;
}

impl Asf64 for UTime {
    fn asf64(&self) -> f64 { self.0 as f64 }
}

impl Asf64 for SysTime {
    fn asf64(&self) -> f64 { self.0 as f64 }
}

impl Asf64 for UDuration {
    fn asf64(&self) -> f64 { self.0 as f64 }
}

impl Asf64 for i64 {
    fn asf64(&self) -> f64 { *self as f64 }
}

impl Asf64 for f64 {
    fn asf64(&self) -> f64 { *self }
}

#[derive(Default,Copy,Clone,Debug)]
pub struct pair<T,E>(pub T,pub E);

impl pair<UTime,SysTime> {
    pub fn error(&self,d:&UClockData) -> f64 {
        self.0.diff(&d.from_sys(&self.1)).asf64()
    }
}

pub fn mean<T:Asf64>(i:&Vec<T>) ->f64 {
    let mut m = 0f64;
    for x in i {
        let xf = x.asf64();
        m += xf;
    }
    m / i.len() as f64
}

pub fn sub_f64<T:Asf64>(i:&Vec<T>,a:f64,v:&mut Vec<f64>) {
    for x in i {
        v.push(x.asf64() - a);
    }
} 

pub fn sdv2<T:Asf64,E:Asf64>(x:&Vec<T>,y:&Vec<E>) -> f64 {
    let mut s = 0f64;
    for  i in 0usize..x.len() {
        s += x[i].asf64() * y[i].asf64();
    }
    s / x.len() as f64
}
